package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.dto.BrandDTO;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌管理
 */
@RestController
@RequestMapping("brand")
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }


    @Autowired
    private CategoryBrandService categoryBrandService;

    /**
     * 根据品牌id查询品牌
     *
     * @param id
     * @return 返回品牌数据
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") long id) {

        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }

    /**
     * 根据品牌集合查询品牌数据
     *
     * @param ids 品牌集合
     * @return 返回品牌集合
     */

    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(ids)));
    }

    /**
     * 根据条件分页查询品牌数据
     *
     * @param key  查询条件
     * @param page 页码
     * @param rows 行数
     * @return
     */

    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows
    ) {
        return ResponseEntity.ok(brandService.queryBrandByPage(key, page, rows));
    }

    /**
     * 根据分类id查询品牌数据
     *
     * @param id 分类id
     * @return 返回品牌集合
     */
    @GetMapping("/of/category")

    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") long id) {

        List<BrandDTO> list = brandService.queryBrandByCategory(id);

        return ResponseEntity.ok(list);

    }

    /**
     *   添加brand数据
     * @param brandDTO  前端输入的品牌数据
     * @return 返回成功状态码
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO) {
        //1.把前端传来的新增数据存储到数据库中
        brandService.saveBrand(brandDTO);
        //2.返回成功的状态码
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /**
     *  根据前端传来的数据更新数据库内容
     * @param brandDTO   brand信息i
     * @return 保存成功状态码为204
     */
    @PutMapping
    public ResponseEntity<Void> updateBrandById(BrandDTO brandDTO) {
      //更新数据
      brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *   根据品牌id删除品牌信息和中间表信息
     * @param id 品牌id
     * @return 无
     */
   @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") long id){
      //1.删除品牌信息
      brandService.removeById(id);
      //2.根据品牌id删除中间表信息
       categoryBrandService.remove(
               new QueryWrapper<CategoryBrand>().eq("brand_Id",id));

       return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
   }


}